package com.wgh.product.service.impl;

import com.wgh.product.entity.shop.ProductAttributeValue;
import com.wgh.product.mapper.ProductAttributeValueMapper;
import com.wgh.product.service.ProductAttributeValueService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 商品属性值服务实现类
 * @author AI Shop
 */
@Service
public class ProductAttributeValueServiceImpl extends ServiceImpl<ProductAttributeValueMapper, ProductAttributeValue> implements ProductAttributeValueService {

    @Override
    public List<ProductAttributeValue> getAttributeValuesByAttributeId(Long attributeId) {
        QueryWrapper<ProductAttributeValue> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("attribute_id", attributeId)
                   .eq("del_flag", 0)
                   .orderByAsc("sort");
        return list(queryWrapper);
    }

    @Override
    public List<ProductAttributeValue> getAttributeValuesByCategoryId(Long categoryId) {
        QueryWrapper<ProductAttributeValue> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_id", categoryId)
                   .eq("del_flag", 0)
                   .orderByAsc("sort");
        return list(queryWrapper);
    }

    @Override
    public ProductAttributeValue getAttributeValueByAttributeIdAndValue(Long attributeId, String value) {
        QueryWrapper<ProductAttributeValue> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("attribute_id", attributeId)
                   .eq("value", value)
                   .eq("del_flag", 0);
        return getOne(queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addAttributeValue(ProductAttributeValue attributeValue) {
        attributeValue.setDelFlag(0);
        return save(attributeValue);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean batchAddAttributeValues(List<ProductAttributeValue> attributeValues) {
        for (ProductAttributeValue value : attributeValues) {
            value.setDelFlag(0);
        }
        return saveBatch(attributeValues);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateAttributeValue(ProductAttributeValue attributeValue) {
        return updateById(attributeValue);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteAttributeValuesByAttributeId(Long attributeId) {
        QueryWrapper<ProductAttributeValue> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("attribute_id", attributeId);
        return remove(queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteAttributeValuesByCategoryId(Long categoryId) {
        QueryWrapper<ProductAttributeValue> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_id", categoryId);
        return remove(queryWrapper);
    }

    @Override
    public List<ProductAttributeValue> getAttributeValuesByAttributeIds(List<Long> attributeIds) {
        QueryWrapper<ProductAttributeValue> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("attribute_id", attributeIds)
                   .eq("del_flag", 0)
                   .orderByAsc("sort");
        return list(queryWrapper);
    }

    @Override
    public boolean checkAttributeValueExists(Long attributeId, String value) {
        QueryWrapper<ProductAttributeValue> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("attribute_id", attributeId)
                   .eq("value", value)
                   .eq("del_flag", 0);
        return count(queryWrapper) > 0;
    }
}